var OPEN_ID = ''//储存获取到openid  
var SESSION_KEY = ''//储存获取到session_key 
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || [];
    logs.unshift(Date.now());
    wx.setStorageSync('logs', logs);
    console.log('logs', logs);
    wx.getUserInfo({
      success: function (resuser) {
        console.log("小程序用户信息")
        console.log(resuser)
      },
      fail: function () {
        // 调用微信弹窗接口
        wx.showModal({
          title: '警告',
          content: '您点击了拒绝授权，将无法正常使用******的功能体验。请10分钟后再次点击授权，或者删除小程序重新进入。',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        })
      }
    })
    wx.login({
      success: function (res) {
        wx.request({/**通过code获取openid**/
          url: getApp().globalData.server_support + "/collect-scrap/queryOpenId",
          data: res.code,
          method: 'POST',
          success: function (res) {
            console.log("res.data----")
            console.log(res)
            console.log(res.data.data.openid)
            OPEN_ID = res.data.data.openid;//获取到的openid 
            getApp().globalData.openid = res.data.data.openid;
            SESSION_KEY = res.data.session_key;//获取到session_key
            wx.request({/**通过code获取openid**/
              url:  getApp().globalData.server_support + "/collect-scrap/register",
              data: '{"userName":"' + OPEN_ID + '","userPassword":"","userType":"0"}',
              method: 'POST',
              success: function (res) {
                console.log(res);
                if (res.data.success) {
                  console.log("登陆成功");
                  getApp().globalData.token = res.data.data.token;
                  var url = '../../pages/order/order';
                  wx.request({/**通过code获取openid**/
                    url:   getApp().globalData.server_support + "/collect-scrap/existUserAddr",
                    header:{
                      'token': getApp().globalData.token
                    },
                    method: 'POST',
                    success: function (res) {
                      console.log(res.data);
                      if (res.data.success) {
                        getApp().globalData.addressStatus = true
                      }else{
                        console.log("用户没填过地址")
                        wx.navigateTo({
                          url: '../../pages/address/address'
                        })
                      }
                    }
                  })
                  wx.switchTab({
                    url: url
                  });
                } else {
                  wx.showLoading({
                    title: '登录失败'
                  });
                  setTimeout(function () {
                    wx.hideLoading();
                  }, 500)
                }
              }
            })
          }
        })
      }
    })  
  },
  globalData: {
    userInfo: null,
    iv: null,
    openid: null,
    code: null,
    token:null,
    //server_support: '10.100.73.101:8080',
    //server_support: 'http://localhost:8080',
    //server_support: '192.168.0.121:8080',
    //server_support: 'chenyanlai.com:8080',
    server_support: 'https://Xcc.home1399.com',
    addressList:null,
    addressStatus:false,
    userType:0
  }
});